package mshop.com.schild_kotlin.adapters

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.orhanobut.logger.Logger
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.activitys.VideoPlayerActivity
import mshop.com.schild_kotlin.entitys.ItemList
import mshop.com.schild_kotlin.entitys.PlayInfoBean
import mshop.com.schild_kotlin.utils.ActivitysUtil
import mshop.com.schild_kotlin.utils.getMiniu

/**
 * Created by mayi on 2018/5/23.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO 首页列表适配器
 */

class HomeAdapter(datas: ArrayList<ItemList>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var datas = ArrayList<ItemList>()

    init {
        this@HomeAdapter.datas = datas
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        var viewItme = LayoutInflater.from(parent?.context).inflate(R.layout.item_home_view, parent, false)
        return ItemViewHold(viewItme)
    }

    override fun getItemCount() = datas.size


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        (holder as? ItemViewHold)?.onBind(datas.get(position))
    }

    class ItemViewHold(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var title_tv: TextView
        var time_tv: TextView
        var item_img: ImageView
        var mContext: Context
        var mEntity:ItemList?=null

        init {
            title_tv = itemView.findViewById(R.id.title_tv)
            item_img = itemView.findViewById(R.id.item_img)
            time_tv = itemView.findViewById(R.id.time_tv)
            mContext = itemView.context
            itemView.setOnClickListener {
                ActivitysUtil.ToVideoPlayerActivity(it.context, mEntity)
            }
        }

        fun onBind(entity: ItemList) {
            mEntity=entity
            title_tv.text = entity.data.description
            time_tv.text = getMiniu(entity.data.duration * 1000)
//            Logger.d("img url :"+entity.data.cover.feed)
            Glide.with(mContext)
                    .load(entity.data.cover.feed)
                    .into(item_img)


//            GlideApp.with(mContext)
//                    .load(entity.data.cover.homepage)
//                    .placeholder(R.drawable.abc_btn_check_material)
//                    .error(R.mipmap.ic_launcher)
////                    .miniThumb()
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .into(item_img)
        }

    }

}